package Control;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import Entity.MsgPackage;
import Entity.User;
import GUI.UpdateAuthorization_GUI;
import GUI.ScreenManager;

/**
 * Controls the instance of Update authorization
 * This class sends and gets data from the server 
 * and display it on the panel.
 * @author Nofar
 *
 */
public class AuthorizationControl {
	private UpdateAuthorization_GUI AuthGui;
	private MsgPackage AuthMsg;
	private User user;
	
	/**
	 * the constructor
	 */
	public AuthorizationControl(){
		AuthGui= new  UpdateAuthorization_GUI();
		AuthMsg= new MsgPackage();
		user= new User();
	}
	
	/**
	 * 
	 * @return UpdateAuthorization_GUI
	 */
	public UpdateAuthorization_GUI getAuthGui(){
		return AuthGui;
	}
		
	/**
	 * This method sends authorization request to the server
	 *  The server sends an answer (received from query from database) 
	 *  and the control display it in a message
	 * @return boolean
	 */
	public boolean updateAuthorization_pressed(){
		String x =AuthGui.getUser_textField().getText();
		String y =(String) AuthGui.getAuthorization().getSelectedItem();
		if((x.equals(""))||y.equals("") )
		{
			JOptionPane.showMessageDialog(null, "Not all the fileds are filled", "ERROR", JOptionPane.ERROR_MESSAGE);
              return false;
		}
		
		user.setUserName(Integer.parseInt(x));
		
		if (y.equals("supervisor")){
			user.setRole(2);
			AuthMsg.setObject(user);
			AuthMsg.setOp_code(OP_CODE.UPDATE_AUTHORIZATION_SUP_OR_CHAIR);

			ScreenManager.getConnection().accept(AuthMsg);
			Object o = ScreenManager.getConnection().cclient.getMessage();
			Integer b= (Integer) o;
			if (b==1){
			JOptionPane.showMessageDialog(null, "The authrization updated successfully", "", JOptionPane.INFORMATION_MESSAGE);
			return true;	
			}
			else if (b==0){
				JOptionPane.showMessageDialog(null, "illegal swap ", "Error", JOptionPane.ERROR_MESSAGE);		
				return false;
			}
			else if (b==2){
				JOptionPane.showMessageDialog(null, "user added and authorization updated successfully", "", JOptionPane.INFORMATION_MESSAGE);
				return false;
			}
	}
		
		
		else if (y.equals("chairman")){
			user.setRole(3);
			AuthMsg.setObject(user);
			AuthMsg.setOp_code(OP_CODE.UPDATE_AUTHORIZATION_SUP_OR_CHAIR);
			
			ScreenManager.getConnection().accept(AuthMsg);
			Object o = ScreenManager.getConnection().cclient.getMessage();
			Integer b= (Integer) o;
				if (b==1){
				JOptionPane.showMessageDialog(null, "The authrization updated successfully", "", JOptionPane.INFORMATION_MESSAGE);
				return true;	
				}
				else if (b==0){
					JOptionPane.showMessageDialog(null, "illegal swap ", "Error", JOptionPane.ERROR_MESSAGE);		
					return false;
				}
				else if (b==2){
					JOptionPane.showMessageDialog(null, "user added and authorization updated successfully", "", JOptionPane.INFORMATION_MESSAGE);
					return false;
				}
		}
		
		
		else if (y.equals("committeeMember")){
			user.setRole(4);
			AuthMsg.setObject(user);
			AuthMsg.setOp_code(OP_CODE.UPDATE_AUTHORIZATION_COMM);
			
			ScreenManager.getConnection().accept(AuthMsg);
			Object o = ScreenManager.getConnection().cclient.getMessage();
			Integer b= (Integer) o;
				if (b==0){
					JOptionPane.showMessageDialog(null, "illegal swap ", "Error", JOptionPane.ERROR_MESSAGE);		
					return false;
				}
				else if (b==1){
					JOptionPane.showMessageDialog(null, "the committee id full, you cant add a third member", "Error", JOptionPane.ERROR_MESSAGE);		
					return false;
				}
				else if (b==2){
					JOptionPane.showMessageDialog(null, "The authrization updated successfully", "", JOptionPane.INFORMATION_MESSAGE);
					return true;	
				}
				else if (b==3){
					JOptionPane.showMessageDialog(null, "user added and authorization updated successfully", "", JOptionPane.INFORMATION_MESSAGE);
					return false;
				}
		}
		
		
		else if (y.equals("informationEngineer")){
			user.setRole(8);
			AuthMsg.setObject(user);
			AuthMsg.setOp_code(OP_CODE.UPDATE_AUTHORIZATION);
			
			ScreenManager.getConnection().accept(AuthMsg);
			Object o = ScreenManager.getConnection().cclient.getMessage();
			Boolean b= (Boolean) o;
				if (b){
					JOptionPane.showMessageDialog(null, "The authrization updated successfully", "", JOptionPane.INFORMATION_MESSAGE);
					return true;	
				}
				else{
					JOptionPane.showMessageDialog(null, "the user is alreday exist", "Error", JOptionPane.ERROR_MESSAGE);		
					return false;
				}
		}
		return true;
	}
}
		
		
		
